# -*- coding: utf-8 -*-
import os
import time

import pandas as pd

from log.logTools import logger_config
from pipe.readExcel import exportExcel,save_json
import csv

def readTxt(filepath):
    # df = pd.read_csv(filepath,names=['col1'],header=None) # 获取日期数据
    df = pd.read_csv(filepath,header=None) # 获取日期数据
    return df

# print(df)
def spiltTxt(filepath,excelName,loginfileName):
    df = readTxt(filepath)
    print(len(df))

    exportPath = os.path.split(os.path.abspath(filepath))[0]
    now_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())
    log_path=exportPath+'./'+loginfileName+now_time+'.txt'
    logger = logger_config(log_path, logging_name='长度不足13的数据')

    result=[]
    a = []
    for ii in df.index:

        if df.iloc[ii].values[0].find('批')!=-1:
            # todo：包含批次的内容
            # print('---------------', ii)
            # print(df.iloc[ii])
            a.append(ii)
            a.append(ii+1)
            result.append(a)
            a = []
        else:
            # todo：计数
            a.append(ii)

        # if (ii != 0 and ii % 13 !=0):
        #    a.append(ii)
        # else:
        #     if ii != 0:
        #         a.append(ii)
        #         result.append(a)
        #         a = []
    print(result)
    # print(df.iloc(result))
    # print(df)
    columns = ['序号', '发电客户名称', '接入电网', '项目名称', '能源类型', '并网规模(KW)', '全部机组并网时间',
               '电价类型', '上网电价(含税)(元/千瓦时)', '列入规模管理年限(年)', '业务类型', '纳入目录时间',
               '纳入目录批次']
    dfs=[]
    for i in result:
        # print(i)
        df1=df[i[0]:i[len(i)-1]]
        # df1=df[i[0]:i[len(i)]]
        # print(i[0],i[len(i)-1])
        # print(df1)
        df2=df1.T
        if len(df2.columns)<13:
            # new_df = pd.DataFrame(df2, columns=columns)
            logger.info(df2)
            save_json(filepath,df2,'导出txt')
        else:
            df2.columns=columns
            new_df = df2
        # df2.rename(columns={'0','序号'},inplace=True)
        # df2.columns = ['发电客户名称','接入电网','项目名称','能源类型','并网规模(KW)','全部机组并网时间','电价类型','上网电价(含税)(元/千瓦时)','列入规模管理年限(年)','业务类型','纳入目录时间','纳入目录批次']
        # df2.columns = ['序号','发电客户名称','接入电网','项目名称','能源类型','并网规模(KW)','全部机组并网时间','电价类型','上网电价(含税)(元/千瓦时)','列入规模管理年限(年)','业务类型','纳入目录时间','纳入目录批次']
        dfs.append(new_df)
        # print(df2)
    # print(dfs)
    df3=pd.concat(dfs)
    # print(df3)
    exportExcel(filepath,df3,excelName)

        # print(ii)
        # date_time = df.loc[ii].values
        # print(date_time)

if __name__ == '__main__':

    # filepath=r'E:\下载\转Excel.txt'
    # filepath = r'E:\下载\转Excel.txt'
    filepath = r'E:\项目相关\13.可再生能源补贴\殷甲伟\可再生能源项目政策文件\新能源云\非自然人分布式\非自然人分布式补贴清单_规律13项.txt'
    loginfileName='日志'
    spiltTxt(filepath,"合并数据",loginfileName)


